package activity

import (
	"context"
	"math"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"
	"gitee.com/liuxuezhan/ar-platform/admin-api/pkg/code"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/client/activityinfo"
	"github.com/zeromicro/go-zero/core/logc"
	"github.com/zeromicro/go-zero/core/logx"
)

type ActivityListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 列表
func NewActivityListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ActivityListLogic {
	return &ActivityListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ActivityListLogic) ActivityList(req *types.ActivityInfoListReq) (resp *types.ActivityInfoListDataRes, err error) {
	resData := make([]types.ActivityInfoListItemRes, 0)

	u, err := l.svcCtx.SettingService.Activity.ActivityInfoList(l.ctx, &activityinfo.ActivityInfoListReq{Page: req.Page, Size: req.Size, MerchantId: 1})

	if err != nil {
		logc.Error(l.ctx, "ActivityList err:", err)
		return nil, err
	}

	if u.Code != int64(code.SettingRpcSuccessful.Code()) {
		return nil, code.BusinessFailure
	}

	for _, v := range u.Data.List {
		resData = append(resData, types.ActivityInfoListItemRes{
			Id:          v.Id,
			MerchantId:  v.MerchantId,
			Title:       v.Title,
			JumpType:    v.JumpType,
			Status:      int64(v.Status),
			Cover:       v.Cover,
			ActivityPic: v.ActivityPic,
			Sort:        v.Sort,
		})
	}

	resp = &types.ActivityInfoListDataRes{
		List: resData,
		Pagination: types.Pagination{
			PageSize:  int32(req.Size),
			Page:      int32(req.Page),
			Total:     u.Data.Count,
			TotalPage: int64(math.Ceil(float64(u.Data.Count) / float64(req.Size))),
		},
	}

	return resp, nil
}
